package com.example.oasystem.controller.userPanel;

import com.example.oasystem.config.ConstantProperties;
import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaUserService;
import com.example.oasystem.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.UUID;

@Controller
public class UserPanelController {

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private ConstantProperties constantProperties;
    @Autowired
    private AoaUserService aoaUserService;
    @Autowired
    private AoaUserDao aoaUserDao;

    @RequestMapping("/saveUser")
    public String updateUser(AoaUser aoaUser, HttpServletRequest request, HttpServletResponse response, MultipartFile filePath, Model model) throws Exception {

        String uploadUrl = null;
        try {
            if (null != filePath) {
                String filename = filePath.getOriginalFilename();
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(filePath.getBytes());
                    os.close();
                    filePath.transferTo(newFile);
                    //上传到OSS
                    uploadUrl = constantProperties.getHead() + aliyunOSSUtil.upload(newFile) + constantProperties.getEnd();
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // 获取session中user
        AoaUser sessionUser = (AoaUser) request.getSession().getAttribute("user");
        // 将session中sessionUser的其他值赋给aoaUser
        aoaUser.setUserId(sessionUser.getUserId());
        aoaUser.setImgPath(sessionUser.getImgPath());
        System.out.println("uploadUrl = " + uploadUrl);
        if (uploadUrl != null) {
            aoaUser.setImgPath(uploadUrl);
            // 更新数据库头像字段
            aoaUserService.updateImgPathById(aoaUser);
            AoaUser aoaUser2 = aoaUserDao.selectById2(sessionUser.getUserId());
            aoaUser2.setAoaDept(sessionUser.getAoaDept());
            aoaUser2.setAoaRole(sessionUser.getAoaRole());
            aoaUser2.setAoaPosition(sessionUser.getAoaPosition());
            request.getSession().setAttribute("user", aoaUser2);
        }
//        aoaUser.setAoaDept(sessionUser.getAoaDept());
//        aoaUser.setAoaRole(sessionUser.getAoaRole());
//        aoaUser.setAoaPosition(sessionUser.getAoaPosition());
//        aoaUser.setRoleId(sessionUser.getRoleId());
        // 判断密码是否为空
        if (("".equals(aoaUser.getPassword()) || aoaUser.getPassword() == null) && ("".equals(aoaUser.getPassword1()) || aoaUser.getPassword1() == null)) {
            aoaUserService.updateAoaUserNoPwd(aoaUser);
            // 更新session中user
            AoaUser aoaUser1 = aoaUserDao.selectById2(sessionUser.getUserId());
            aoaUser1.setAoaDept(sessionUser.getAoaDept());
            aoaUser1.setAoaRole(sessionUser.getAoaRole());
            aoaUser1.setAoaPosition(sessionUser.getAoaPosition());
            request.getSession().setAttribute("user", aoaUser1);
            return "/userpanel";
        } else {
            aoaUserService.updateAoaUserHasPwd(aoaUser);
            // 更新session中user
            request.getSession().removeAttribute("user");
            // 跳出
            PrintWriter out = response.getWriter();
            out.print("<script>window.parent.location.href='/';</script>");
            out.flush();
            return null;
        }
    }
}
